package cn.lixufeng.system.controller;

import cn.lixufeng.basic.utils.AjaxResult;
import cn.lixufeng.basic.utils.PageList;
import cn.lixufeng.system.domain.DictionaryType;
import cn.lixufeng.system.query.DictionaryTypeQuery;
import cn.lixufeng.system.service.IDictionaryTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dictionaryType")
public class DictionaryTypeController {

    @Autowired
    private IDictionaryTypeService dictionaryTypeService;

    /**
     * 增加或修改
     * @param dictionaryType
     * @return
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody DictionaryType dictionaryType){
        try {
            if (dictionaryType.getId() == null) {
                dictionaryTypeService.add(dictionaryType);
            } else {
                dictionaryTypeService.update(dictionaryType);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("操作失败");
        }
    }

    /**
     * 按id删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            dictionaryTypeService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("操作失败");
        }
    }

    /**
     * 按id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public DictionaryType findById(@PathVariable("id") Long id){
        return dictionaryTypeService.findById(id);
    }

    /**
     * 查询所有
     * @return
     */
    @GetMapping("listAll")
    public List<DictionaryType> findAll(){
        return dictionaryTypeService.findAll();
    }

    /**
     * 分页和高级查询
     * @param dictionaryTypeQuery
     * @return
     */
    @PostMapping("/listByPage")
    public PageList<DictionaryType> findPage(@RequestBody DictionaryTypeQuery dictionaryTypeQuery){
        return dictionaryTypeService.findPage(dictionaryTypeQuery);
    }

    /**
     * 批量删除
     * @param ids 是需要删除的id的集合，前台传的是数组，后台可以用数组或者集合来接收
     * @return
     */
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
            dictionaryTypeService.batchDelete(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("操作失败");
        }
    }

}
